package com.wc.alorithm.string.trietree;

/**
 * @Author congege
 * @Description 字典树 trieTree
 * @Date Created in 2023/12/1-19:55
 */
public class Main {

    static class TreeNode {
        final static int MAX_SIZE = 26;
        char data;
        boolean isEnd;
        TreeNode[] children;

        public TreeNode() {
            isEnd = false;
            children = new TreeNode[MAX_SIZE];
        }
    }

    private static void createTree(TreeNode node, String word) {
        for (char c : word.toCharArray()) {
            int loc = c - 'a';
            if (node.children[loc] == null) {
                node.children[loc] = new TreeNode();
                node.children[loc].data = c;
            }
            node = node.children[c - 'a'];
        }
        node.isEnd = true;
    }

    private static boolean find(TreeNode node, String word) {
        for (char c : word.toCharArray()) {
            int loc = c - 'a';
            if (node.children[loc] != null) {
                node = node.children[loc];
            } else {
                return false;
            }
        }
        return node.isEnd;
    }

    public static void main(String[] args) {
        String[] ss = new String[]{"java", "ps", "php", "ui", "css", "js"};

        TreeNode root = new TreeNode();
        for (String s : ss) {
            createTree(root,s);
        }
        System.out.println(find(root, "java"));
        System.out.println(find(root,"u"));
    }
}
